﻿using System;
using System.Text.RegularExpressions;
using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;
using Common;

namespace IssueCommon
{
    internal class CommonShtmlIssue : IssueBase, IDisposable
    {
        public CommonShtmlIssue(SessionUser sessionUser)
        {
            base.SessionUser = sessionUser;
        }

        /// <summary>
        ///生成shtml公共页面
        /// </summary>
        public void CreateCommonInclude(int intWSID)
        {
            using (TemplateFactory objFactory = new TemplateFactory())
            {
                string strSql = string.Format("0=0 and  intType ={0} and intState={1} and intWSID={2}", (int)Utility.TemplateType.Public, (int)Utility.TemplateState.Use, intWSID);
                Template objTemplate = objFactory.CreatorBySql(strSql);
                if (objTemplate != null)
                {
                    foreach (Model.Data.TemplateData.TemplateRow objRow in objTemplate.Data.Template.Rows)
                    {
                        string strTemplate = Template.GetTemplateString(objRow.intTemplateID,base.SessionUser);

                        //生成公共模板，插件一解析
                        string strRegexNoPage = string.Format("{0}(?!.+<pagesize>[0-9]+</pagesize>.+)([\\d\\D]+?){1}", Model.Common.Plugin.PluginBeginTag, Model.Common.Plugin.PluginEndTag);
                        string strHtml = Regex.Replace(strTemplate, strRegexNoPage, new MatchEvaluator(base.ReplacePlugin), RegexOptions.IgnoreCase);

                        string strPath = string.Format("{0}\\C_{1}.htm", base.SessionUser.IssuePath + "\\common", objRow.intTemplateID);
                        base.GenerateHtmlFile(strPath, strHtml);
                    }
                }
            }
        }
    }
}